package com.dn.apiopen.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dn.apiopen.common.ErrorCode;
import com.dn.apiopen.exception.BusinessException;
import com.dn.apiopen.mapper.InterfaceInfoMapper;
import com.dn.apiopen.service.InterfaceInfoService;
import com.dn.dncommon.model.entity.InterfaceInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author 18398
* @description 针对表【interface_info(接口信息)】的数据库操作Service实现
* @createDate 2024-03-17 16:22:31
*/
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
    implements InterfaceInfoService {

    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = interfaceInfo.getName();
        String description = interfaceInfo.getDescription();
        String url = interfaceInfo.getUrl();
        String method = interfaceInfo.getMethod();
        if (StringUtils.isNotBlank(method)){
            interfaceInfo.setMethod(method.toUpperCase());
        }
        String requestHeader = interfaceInfo.getRequestHeader();
        String responseHeader = interfaceInfo.getResponseHeader();
        // 创建时，参数不能为空
        if (add) {
            if (StringUtils.isAnyBlank(name, description, url, method)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        // 有参数则校验
        if (StringUtils.isNotBlank(name) && name.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "名称过长");
        }
        List<String> list = Arrays.asList("GET", "POST", "PUT", "DELETE");
        if (StringUtils.isNotEmpty(method) && !list.contains(method.toUpperCase())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求方法错误");
        }
    }


}




